package com.training.persistence.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.training.persistence.entity.PlayerGame;

@Transactional(propagation=Propagation.REQUIRED, readOnly=false)

public class PlayerGameDaoImpl implements PlayerGameDao {

	@Autowired
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public PlayerGame savePlayerGame(PlayerGame playergame) {
		this.getSession().save(playergame);
		return playergame;
	}

	public PlayerGame updatePlayerGame(PlayerGame playergame) {
		this.getSession().saveOrUpdate(playergame);
		return playergame;
	}

	public List<PlayerGame> findAllPlayerGames() {
		return this.getSession().createQuery("from PLAYERGAME").list();
	}

	public PlayerGame findPlayerGameById(Long id) {
		return null;
	}

	public PlayerGame findPlayerGameByPlayerGameId(Integer playergameId) {
		List<PlayerGame> Playergames = this.getSession()
				.createQuery("from PlayerGame p where p.PlayergameId = :PlayergameId")
				.setInteger("PlayergameId", playergameId).list();
		if (Playergames.isEmpty()) {
			return null;
		} else {
			return Playergames.get(0);
		}
	}
    
    public Session getSession(){
    	return sessionFactory.getCurrentSession();
    }
    
    
}
